<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/8/6
 * Time: 2:06
 */

namespace app\admin\model;


class ApprovalPurchase extends Base
{
    /**
     * 获取列表
     * $t : 1获取列表 2导出
     */
    public function getList($t,$map,$query = [])
    {
        if($t == 1) {
            $list = $this->where($map)->order('apply_time desc')->paginate(10,false,['query' => $query]);
            foreach ($list as $key => $value) {
                $status = [0 => "待审批", 1 => "审批通过", 2=> "审批不通过", 3=>"进行中"];
                $value['status_text'] = $status[$value['status']];
                $user = $this->getUser($value['apply_user']);
                $value['apply_name'] = $user ? $user['name'] : "无该用户信息";
                $value['approval_user'] = $this->getApprovalUser(3,$value);
                $depart = $this->getDepartment($value['apply_department']);
                $value['apply_department_name'] = !empty($depart) ? $depart['name'] : '--';
                $value['apply_time_text'] = date("Y-m-d H:i",$value['apply_time']);
                $detail = ApprovalPurchaseDetail::where(['pid' => $value['id']])->select();
                foreach ($detail as $v) {
                    $v['arrival_time_text'] = date("Y-m-d H:i",$v['arrival_time']);
                }
                $value['detail'] = $detail;
            }
        }else {
            $list = [];
            $main = $this->where($map)->order('apply_time desc')->select();
            foreach ($main as $key => $value) {
                $status = [0 => "待审批", 1 => "审批通过", 2=> "审批不通过", 3=>"进行中"];
                $value['status_text'] = $status[$value['status']];
                $user = $this->getUser($value['apply_user']);
                $value['apply_name'] = $user ? $user['name'] : "无该用户信息";
                $value['approval_user'] = $this->getApprovalUser(3,$value);
                $depart = $this->getDepartment($value['apply_department']);
                $value['apply_department_name'] = !empty($depart) ? $depart['name'] : '--';
                $value['apply_time_text'] = date("Y-m-d H:i",$value['apply_time']);
                $value['type'] = 1;
                $list[] = $value;
                $detail = ApprovalPurchaseDetail::where(['pid' => $value['id']])->select();
                $i = 1;
                $msg = [];
                foreach ($detail as $k => $v) {
                    $v['arrival_time_text'] = date("Y-m-d H:i",$v['arrival_time']);
                    $v['type'] = 3;
                    $msg[0] = [
                        'pid' => $value['id'],
                        'name' => "物品名称",
                        'model' => "型号",
                        'company' => "单位",
                        'number' => "数量",
                        'arrival_time_text' => "到货时间",
                        'content' => "备注",
                        'type' => 2
                    ];
                    $msg[$i] = $detail[$k];
                    $i++;
                }
                foreach ($msg as $vv) {
                    $list[] = $vv;
                }
            }
        }
        return $list;
    }
}